package com.von.nhcf.service.impl;

import com.von.nhcf.entity.Auth;
import com.von.nhcf.entity.AuthExample;
import com.von.nhcf.mapper.AuthMapper;
import com.von.nhcf.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author
 */
@Service
public class AuthServiceImpl implements AuthService {
    @Autowired
    private AuthMapper authMapper;

    @Override
    public List<Auth> getAll() {
        return authMapper.selectByExample(new AuthExample());
    }

    @Override
    public List<Integer> getAssignedAuthByRoleid(Integer roleId) {
        return authMapper.selectAssignedAuthByRoleid(roleId);
    }

    @Override
    public void doAssignAuth(Map<String, List<Integer>> requestBody) {
        // 获取map中的键值对
        List<Integer> roleIdList = requestBody.get("roleId");
        Integer roleId = roleIdList.get(0);
        List<Integer> authIdArray = requestBody.get("authIdArray");
        // 清除旧的权限关系
        authMapper.deleteOldAuth(roleId);
        // 插入新的权限关系
        if (authIdArray != null && authIdArray.size() > 0) {
            authMapper.insertNewAuth(roleId, authIdArray);
        }
    }

    @Override
    public List<String> getAssignedAuthNameByAdminId(Integer adminId) {
        return authMapper.selectgetAssignedAuthNameByAdminId(adminId);
    }
}

